﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InHD.model;
using InHD.utilities;
using System.Data;

namespace InHD.dataaccess
{
    class AccountRepository
    {
        #region Fields

        readonly List<Account> _accounts;
        //For DB Utils
        private DBUltils _db;
        string[] _paramName;
        string[] _value;
        DBUltils.ParamType[] _paramType;

        #endregion // Fields

        #region Constructor

        /// <summary>
        /// Creates a new repository of Staffs.
        /// </summary>
        public AccountRepository()
        {
            _accounts = new List<Account>();
            this._db = new DBUltils();
            if (!_db.connectDB(Constants.Security.DefaultDBUser, Constants.Security.DefaultDBPassword))
                throw new ArgumentNullException(Constants.Messages.CannotConnectDB);
        }

        #endregion // Constructor

        #region Public Interface
        /// <summary>
        /// Returns a shallow-copied list of all Account in the repository.
        /// </summary>
        public List<Account> GetAccounts()
        {
            return new List<Account>(_accounts);
        }

        public string DeleteAccount(string accUNK)
        {
            bool found = false;
            foreach (Account acc in this._accounts)
            {
                if (acc.AccountUNK.Equals(accUNK))
                {
                    this._accounts.Remove(acc);
                    found = true;
                    break;
                }
            }
            if (!found)
                return "UNK is not exist";
            try
            {
                if (_db == null)
                    throw new ArgumentNullException(Constants.Messages.DBNotInitialized);
                if (accUNK == null)
                    throw new ArgumentNullException("Null Unique key");
                _paramName = new string[]{"@accUNK"};
                _paramType = new DBUltils.ParamType[]{DBUltils.ParamType.Str};
                _value = new string[]{accUNK};

                _db.queryDB("DeleteAccount", _paramName, _paramType, _value, DBUltils.QueryType.Delete);
            }
            catch(Exception e)
            {
                return e.Message;
            }
//            System.Windows.MessageBox.Show("Delete Account successfully!!!");
            return string.Empty;
        }

        /// <summary>
        /// Connect vao Database de lay toan bo du lieu ve cac User hien co trong he thong
        /// </summary>
        public string LoadAllAccounts()
        {
            try
            {
                //// Load all Account from database
                if (_db == null)
                    throw new ArgumentNullException(Constants.Messages.DBNotInitialized);
                _paramName = new string[] { };
                _paramType = new DBUltils.ParamType[] { };
                _value = new string[] { };
                DataSet ds = _db.queryDB(Constants.StoreProcedures.Account.GetAllAccounts, _paramName, _paramType, _value, DBUltils.QueryType.Select);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Account acc = Account.CreateAccount(Convert.ToString(dr["accUNK"]),
                                                        Convert.ToInt64(dr["accCode"]),
                                                        Convert.ToString(dr["accType"]),
                                                        Convert.ToInt32(dr["level"]),
                                                        Convert.ToInt32(dr["parentAcc"]),
                                                        Convert.ToString(dr["accDescription"]),
                                                        Convert.ToDouble(dr["TKCBalance"]),
                                                        Convert.ToDouble(dr["TKNBalance"]),
                                                        Convert.ToDateTime(dr["lastAccess"])
                                                        );
                    _accounts.Add(acc);
                }
            }
            catch (Exception e)
            {
                Logger.RecordMessage(e, MessageType.Error);
                return e.Message;
            }
            return string.Empty;
        }
        #endregion
    }
}
